package NBU::Robot;

require 5.002;

use strict;
use Carp;

BEGIN {
  use Exporter   ();
  use AutoLoader qw(AUTOLOAD);
  use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
  $VERSION =     '0.01';
  @ISA =         qw();
  @EXPORT =      qw();
  @EXPORT_OK =   qw();
  %EXPORT_TAGS = qw();
}

my @farm;

my %robotTypes = (
  8 => 'TLD',
);

sub new {
  my $class = shift;
  my $robot = {};

  bless $robot, $class;

  if (@_) {
    $robot->{ID} = shift;
    my $type;
    if (exists($robotTypes{$type = shift})) {
      $robot->{TYPE} = $robotTypes{$type};
    }
    else {
      $robot->{TYPE} = $type;
    }
    my $hostName = shift;
    my $host = NBU::Host->byName($hostName);
    $robot->{CONTROLHOST} = $host;
  }
  $robot->{DEVICEPATH} = undef;

  $robot->{MODEL} = "STK 9740";
  $robot->{CAPACITY} = 326;
  $robot->{MAILSLOTSIZE} = 14;

  $robot->{SLOTS} = {};

  $farm[$robot->{ID}] = $robot;
  return $robot;
}

sub byID {
  my $class = shift;
  my $id = shift;

  return $farm[$id];
}

sub id {
  my $self = shift;

  return $self->{ID};
}

sub type {
  my $self = shift;

  return $self->{TYPE};
}

sub host {
  my $self = shift;

  return $self->{HOST};
}


sub insert {
  my $self = shift;
  my $slotList = $self->{SLOTS};
  my ($position, $volume) = @_;

  if (exists($$slotList{$position}) && ($$slotList{$position} != $volume)) {
    print STDERR "Slot $position in ".$self->id." already filled; no room for ".$volume->id."\n";
  }
  else {
    $$slotList{$position} = $volume;
  }
  return $$slotList{$position};
}

sub slot {
  my $self = shift;
  my $slotList = $self->{SLOTS};
  my $position = shift;

  if (@_) {
    return $self->insert($position, shift);
  }
  return $$slotList{$position};
}

sub capacity {
  my $self = shift;

  return $self->{CAPACITY};
}

sub slotList {
  my $self = shift;
  my $slotList = $self->{SLOTS};
  my @list;

  for my $s (1..$self->capacity) {
    push @list, $$slotList{$s};
  }

  return @list;
}

sub mediaList {
  my $self = shift;
  my $slotList = $self->{SLOTS};

  return (values %$slotList);
}

sub farm {
  my $Class = shift;

  return (@farm);
}

sub importCAP {
  my $self = shift;

# vmupdate -rn <number> -rt <type> -interactive -empty_ie
}

sub updateInventory {
  my $self = shift;

# vmupdate -rn <number> -rt <type> -M $master -mt $density -use_barcode_rules -rh $jukebox_robot_host{$jukebox} -vh $master" ;
}

1;

__END__
